﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WebService.Proxy
{
    public class EarthquakeServiceProxy : IEarthquakeServiceProxy
    {
        const string getDataUrl = "http://localhost:46508/api/data";

        public async Task<IEnumerable<EarthquakeEvent>> GetDataAsync()
        {
            var http = new HttpClient();
            var jsonString = await http.GetStringAsync(getDataUrl);

            var json = JArray.Parse(jsonString);
            var children = json.Children();

            return from child in children
                   select new EarthquakeEvent
                   {
                       Title = (string)child["Region"],
                       Magnitude = (decimal)child["Magnitude"],
                       Latitude = (decimal)child["Lat"],
                       Longitude = (decimal)child["Lon"],
                       Datetime = (DateTime) child["Datetime"]
                   };
        }
    }
}
